﻿using Microsoft.VisualStudio.TestTools.UITest.Extension;
using Microsoft.VisualStudio.TestTools.UITesting;
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;

namespace AXExtension
{
	static class AXUtils
	{
		public static UITechnologyManager Manager { get; set; }

		static AXUtils()
		{
			Manager = Playback.GetCoreTechnologyManager("UIA");
		}

		/// <summary>
		/// Requires manager to have been set prior to usage.
		/// </summary>
		public static IUITechnologyElement Parent(this IUITechnologyElement element)
		{
			if (Manager == null)
			{
				return null;
			}
			return Manager.GetParent(element);
		}

		/// <summary>
		/// Requires manager to have been set prior to usage.
		/// </summary>
		public static IUITechnologyElement Child(this IUITechnologyElement element, string name, string controltype)
		{
			if (Manager == null)
			{
				return null;
			}
			var enumerator = Manager.GetChildren(element, new PropertyCondition("Name", name));
			while (enumerator.MoveNext())
			{
				if ((enumerator.Current as IUITechnologyElement).Name != null &&
					(enumerator.Current as IUITechnologyElement).Name.Contains(name) &&
					(enumerator.Current as IUITechnologyElement).ControlTypeName == controltype)
				{
					return enumerator.Current as IUITechnologyElement;
				}
			}
			return null;
		}

		/// <summary>
		/// Requires manager to have been set prior to usage.
		/// </summary>
		public static List<IUITechnologyElement> Children(this IUITechnologyElement element)
		{
			if (Manager == null)
			{
				return new List<IUITechnologyElement>();
			}
			var children = new List<IUITechnologyElement>();
			var enumerator = Manager.GetChildren(element, new PropertyCondition("Name", "", PropertyConditionOperator.Contains));
			while (enumerator.MoveNext())
			{
				children.Add(enumerator.Current as IUITechnologyElement);
			}
			return children;
		}

		/// <summary>
		/// Requires manager to have been set prior to usage.
		/// </summary>
		public static IUITechnologyElement Child(this IUITechnologyElement element, string controltype)
		{
			if(Manager == null)
			{
				return null;
			}
			var enumerator = Manager.GetChildren(element, new PropertyCondition("ControlType", controltype));
			while (enumerator.MoveNext())
			{
				if (string.IsNullOrWhiteSpace((enumerator.Current as IUITechnologyElement).Name) &&
					(enumerator.Current as IUITechnologyElement).ControlTypeName == controltype)
				{
					return enumerator.Current as IUITechnologyElement;
				}
			}
			return null;
		}

		/// <summary>
		/// Requires manager to have been set prior to usage.
		/// </summary>
		public static IUITechnologyElement Child(this IUITechnologyElement element, int index)
		{
			if (Manager == null)
			{
				return null;
			}

			var enumerator = Manager.GetChildren(element, new PropertyCondition("Name", "", PropertyConditionOperator.Contains));
			bool worked = false;
			for (int i = 0; i <= index && (worked = enumerator.MoveNext()); ++i) ;
			if (worked)
			{
				return enumerator.Current as IUITechnologyElement;
			}
			return null;
		}

		public static void LoadManager(string name)
		{
			Manager = Playback.GetCoreTechnologyManager(name);
		}

		[DllImport("user32.dll", SetLastError = false)]
		static extern IntPtr GetDesktopWindow();

		public static IUITechnologyElement Desktop
		{
			get
			{
				return Manager.GetElementFromWindowHandle(GetDesktopWindow());
			}
		}
	}
}
